<template>
	<div class="logs-page">
		<!--查询表单-->
		<el-card class="box-card mb10" shadow="never">
			<el-form ref="searchFormRef" class="search-form" :inline="true" label-position="left" label-width="75px" :model="searchForm">
				<el-row :gutter="30" justify="start">
					<el-col :span="6">
						<el-form-item class="form-item" label="用户账号:" prop="userName">
							<el-input v-model="searchForm.userName" placeholder="请输入用户账号" />
						</el-form-item>
					</el-col>
					<el-col :span="6">
						<el-form-item class="form-item" label="角色状态:" prop="status">
							<el-select v-model="searchForm.status" class="el-form-item" clearable placeholder="请选择角色状态">
								<el-option label="成功" value="0" />
								<el-option label="失败" value="1" />
							</el-select>
						</el-form-item>
					</el-col>
					<el-col :span="6">
						<el-form-item class="form-item" label="地址:" prop="ipaddr">
							<el-input v-model="searchForm.ipaddr" placeholder="请输入用户地址" />
						</el-form-item>
					</el-col>

					<el-col :span="6">
						<div class="search-btn">
							<el-button type="primary" @click="handleSearch">查询</el-button>
							<el-button @click="handleReset">重置</el-button>
						</div>
					</el-col>
				</el-row>
			</el-form>
		</el-card>
		<el-card class="box-card" shadow="never">
			<static-table :tableData="tableData" rowId="Id" :loading="loading" :seqConfig="{ startIndex: (pageNum - 1) * pageSize }" @on-selection-change="handleSelectionChange">
				<template #vux-search>
					<div class="vxe-action">
						<el-row :gutter="12">
							<el-button class="ml6 mb10" v-has="'system:login:log:clear'" type="warning" @click="handleClear()">批量删除</el-button>
						</el-row>
					</div>
				</template>
				<template #vux-column>
					<vxe-column field="selection" fixed="left" type="checkbox" width="60" />
					<vxe-column type="seq" title="序号" width="60" />
					<vxe-column field="userName" min-width="120" show-overflow="tooltip" title="用户账号" />
					<vxe-column field="status" min-width="120" title="状态">
						<template #default="{ row }">
							<span>{{ row.status == '0' ? '成功' : '失败' }}</span>
						</template>
					</vxe-column>
					<vxe-column field="ipaddr" min-width="120" show-overflow="tooltip" title="地址" />
					<vxe-column field="loginLocation" min-width="120" show-overflow="tooltip" title="登录地点" />
					<vxe-column field="browser" min-width="120" show-overflow="tooltip" title="浏览器类型" />
					<vxe-column field="os" min-width="120" show-overflow="tooltip" title="操作系统" />
					<vxe-column field="msg" min-width="120" show-overflow="tooltip" title="描述" />
					<vxe-column field="loginTime" min-width="120" show-overflow="tooltip" title="访问时间" />
					<vxe-column field="params" fixed="right" min-width="120" title="操作">
						<template #default="{ row }">
							<div class="vxe-action">
								<a @click="handleDelete(row)" v-has="'system:login:log:clear'">删除</a>
							</div>
						</template>
					</vxe-column>
				</template>
			</static-table>
			<el-pagination background :current-page="pageNum" :layout="layout" :page-size="pageSize" :total="dataCount" @current-change="handleCurrentChange" @size-change="handleSizeChange" />
		</el-card>
	</div>
</template>
<script name="LogsManage" setup>
import useTableList from '@/hooks/useTableList'
import global from '@/utils/global'
const state = reactive({
	searchForm: {
		userName: '', //用户账号
		status: '', //状态
		ipaddr: '', //地址
		beginTime: '',
		endTime: '',
	},
	createTimeAry: [],
	searchFormRef: null,
	selectionAry: [],
})
const { searchForm, searchFormRef } = toRefs(state)
const queryUrl = '/meta-system/web/v1/loginLog/list'
const { loading, dataCount, handleInitQuery, getTimeParseAry, handleModalOk, tableData, pageNum, pageSize, layout, handleSizeChange, handleCurrentChange } = useTableList(queryUrl, state.searchForm)
const handleSearch = () => {
	const { beginTime, endTime } = getTimeParseAry(state.createTimeAry)
	state.searchForm.beginTime = beginTime
	state.searchForm.endTime = endTime
	handleInitQuery()
}
const handleReset = () => {
	state.searchFormRef.resetFields()
	state.createTimeAry = []
	handleSearch()
}
const handleDelete = row => {
	global.$baseConfirm(
		'是否确认删除当前选中的数据？',
		'提示',
		() => {
			global.$fetchPostData(`/meta-system/web/v1/loginLog/remove`, { removeIdList: [row.id] }).then(data => {
				if (data.code == 200) {
					global.$baseMessage(data.msg, 'success')
					handleModalOk(2)
				} else {
					global.$baseMessage(data.msg, 'error')
				}
			})
		},
		() => {}
	)
}
// 批量删除

const handleSelectionChange = records => {
	console.log(records)
	const ids = records.map(record => record.id)
	state.selectionAry = ids
	console.log(state.selectionAry)
}
const handleClear = () => {
	if (state.selectionAry.length === 0) {
		global.$baseMessage('请选择要删除的数据', 'warning')
		return
	}
	global.$baseConfirm(
		'是否确认批量删除选中的数据？',
		'提示',
		() => {
			global.$fetchPostData(`/meta-system/web/v1/loginLog/remove`, { removeIdList: state.selectionAry }).then(data => {
				if (data.code === 200) {
					global.$baseMessage(data.msg, 'success')
					handleModalOk(2)
				} else {
					global.$baseMessage(data.msg, 'error')
				}
			})
		},
		() => {}
	)
}
</script>
<style lang="scss" scoped></style>
